package de.openlaboratory.elib.model;

import java.io.IOException;
import java.io.Serializable;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Map;
import java.util.jar.Attributes;

import com.itextpdf.text.pdf.PdfReader;

import de.openlaboratory.elib.core.LogManager;

public class ImportFile implements Serializable
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 7351173923467825778L;
	private static final String FAIL_NAME = "fail";
	
	private Path path;
	private String name;
	private int pages;
	private String mimeType;
	private String extension;
	private String title;
	private String author;

	public ImportFile(Path p)
	{
		this.path = p;
		
		setExtension();
	}
	
	public ImportFile(String name)
	{
		try
		{
			this.path = Paths.get(name);
			
			setExtension();
		}
		catch (Exception p)
		{
			LogManager.logMessage("File creation error - File could not be read [" + name + "]: " + p.getMessage());
		}
	}

	public boolean isDirectory()
	{
		return path.toFile().isDirectory();
	}
	
	public void setName()
	{
		this.name = "file";
	}

	public String getName()
	{
		try
		{
			name = path.getFileName().toString();
		}
		catch (Exception p)
		{
			name = FAIL_NAME;
		}
		return name;
	}
	
	public Path getPath()
	{
		return path;
	}
	
	public String getFullPath()
	{
		return path.toAbsolutePath().toString();
	}

	public void readDocumentInfo()
	{
		if (!getName().equals(FAIL_NAME))
		{
			if (getName().endsWith("pdf"))
			{
				this.mimeType = "application/pdf";
				
				LogManager.logMessage(String.format("File info (%s)", this.path.toAbsolutePath().toString()));
	
				try
				{
					PdfReader reader = new PdfReader(this.path.toAbsolutePath().toString());
					this.pages = reader.getNumberOfPages();
					
			        Map<String, String> info = reader.getInfo();
			        this.title = info.get("Title");
			        this.author = info.get("Author");
			        
			        reader.close();
				}
				catch (IOException e)
				{
					LogManager.logMessage("iText can't read file: " + e.getMessage());
				}
				catch (Exception p)
				{
					LogManager.logMessage("iText error: " + p.getMessage());
				}
			}
		}
	}

	public int getPages()
	{
		return pages;
	}

	public String getMimeType()
	{
		return mimeType;
	}

	public String getTitle()
	{
		return title;
	}

	public String getAuthor()
	{
		return author;
	}

	public String getExtension()
	{
		return extension;
	}

	public void setExtension()
	{
		if (this.path == null) return;
		
		if (this.isFolder())
			this.extension = "folder";
		if (getName().endsWith("pdf"))
			this.extension = "pdf";
		else if(getName().endsWith("doc") || getName().endsWith("docx"))
			this.extension = "word";
		else if(getName().endsWith("epub"))
			this.extension = "epub";
		else if(getName().endsWith("txt"))
			this.extension = "text";
		else
			this.extension = "delete";
	}

	private boolean isFolder()
	{
		if (path.toFile().isDirectory())
			return true;
		else
			return false;
	}
}
